package org.dromara.demo.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.PageData;
import org.dromara.common.web.core.BaseService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.demo.domain.DemoExample4;
import org.dromara.demo.service.DemoExample4ServiceImpl;
import org.dromara.common.mybatis.core.page.TableDataInfo;


/**
 * 单表
 *
 * @author wenrui Su
 * @date 2024-05-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/example4")
public class DemoExample4Controller extends BaseService {

  private final DemoExample4ServiceImpl demoExample4Service;

  /**
   * 查询单表列表
   */
  @SaCheckPermission("demo:example4:list")
  @GetMapping("/list")
  public TableDataInfo<DemoExample4> list(DemoExample4 bo, PageQuery pageQuery) {
    return demoExample4Service.queryPageList(bo, pageQuery);
  }

  /**
   * 导出单表列表
   */
  @SaCheckPermission("demo:example4:export")
  @Log(title = "单表", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(DemoExample4 bo, HttpServletResponse response) {
    List<DemoExample4> list = demoExample4Service.queryList(bo);
    ExcelUtil.exportExcel(list, "单表", DemoExample4. class,response);
  }

  /**
   * 获取单表详细信息
   *
   * @param id 主键
   */
  @SaCheckPermission("demo:example4:query")
  @GetMapping("/{id}")
  public R<DemoExample4> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
    return R.ok(demoExample4Service.queryById(id));
  }

  /**
   * 新增单表
   */
  @SaCheckPermission("demo:example4:add")
  @Log(title = "单表", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public R<Void> add(@Validated(AddGroup.class) @RequestBody DemoExample4 bo) {
    return toAjax(demoExample4Service.insertByBo(bo));
  }

  /**
   * 修改单表
   */
  @SaCheckPermission("demo:example4:edit")
  @Log(title = "单表", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public R<Void> edit(@Validated(EditGroup.class) @RequestBody DemoExample4 bo) {
    return toAjax(demoExample4Service.updateByBo(bo));
  }

  /**
   * 删除单表
   *
   * @param ids 主键串
   */
  @SaCheckPermission("demo:example4:remove")
  @Log(title = "样例单表4", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public PageData remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
    return this.resData(demoExample4Service.deleteWithValidByIds(List.of(ids), true));
  }
}
